const express = require("express");
const app = express();
const http = require("http").Server(app);
const io = require("socket.io")(http);

const port = 3000;

// 监听连接事件
io.on("connection", (socket) => {
  console.log(socket);

  console.log("A user connected");

  // 监听心跳事件
  socket.on("heartbeat", () => {
    socket.emit("heartbeat-ack");
  });

  // 监听断开连接事件
  socket.on("disconnect", () => {
    console.log("A user disconnected");
  });
});

http.listen(port, () => {
  console.log(`Server running on port ${port}`);
});
